//
//  TestMapViewController.m
//  TestMap
//
//  Created by DongTing on 10-5-25.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "TestMapViewController.h"

@implementation TestMapViewController
@synthesize thePOIMapView;


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	UIButton *read = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	read.backgroundColor = [UIColor clearColor];
	read.frame = CGRectMake(100, 200, 60, 30);
	[read setTitle:@"Read" forState:UIControlStateNormal];
	[read addTarget:self action:@selector(readLocalData) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:read];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	
	if (dataArray)
		[dataArray release];
	
    [super dealloc];
}

#pragma mark -
#pragma mark Actions
- (void)readLocalData 
{
	dataArray = [[NSMutableArray alloc] init];
	if (thePOIMapView==nil){
		thePOIMapView = [[POIMapView alloc]initNib:nil bundle:nil];
	}
	
	int count = 0;
	
	for (int i=0;i<1;i++)
	{
	NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",i+1] ofType:@"TXT"];
	NSString *contents = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
	NSArray *contentsArray = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
	
	NSInteger idx;    
	for (idx = 0; idx < contentsArray.count; idx++) {
		NSString* currentContent = [contentsArray objectAtIndex:idx];
		
		NSArray *compArray = [currentContent componentsSeparatedByString:@" "];
		//NSLog(@"string array = %@",compArray);
		
		int arrayCount = [compArray count];
		if(arrayCount == 1)
			continue;
		
		float lat = [[compArray objectAtIndex:arrayCount - 2] floatValue];
		float lon = [[compArray objectAtIndex:arrayCount - 3] floatValue];
		
		NSLog(@"the lat:%f lon:%f",lat,lon);
		
		CLLocationCoordinate2D cordinate;
		cordinate.latitude = lon;
		cordinate.longitude = lat;
		
		NSString *speed = [compArray objectAtIndex:arrayCount - 1];
		
		PlaceMark *poi1= [[PlaceMark alloc] initWithCoordinate:cordinate];
		poi1.titletext = speed;
		poi1.xiaoquID=@"1";
		
		[dataArray addObject:poi1];
		//[poi1 release];
		
		if (count==0) {
			MKCoordinateRegion region;
			MKCoordinateSpan span;
			span.latitudeDelta=0.005;
			span.longitudeDelta=0.005;
			
			cordinate.latitude=[@"31.1856344" floatValue];
			cordinate.longitude=[@"121.517347086639" floatValue];
			
			region.span=span;
			region.center=cordinate;
			thePOIMapView.region = region;
			
			count ++;
		}
	}
	}
	
	thePOIMapView.thePOIArray = dataArray;

	[self presentModalViewController:thePOIMapView animated:YES];
	//[self pushViewController:thePOIMapView animated:YES];
	
}

@end
